package javaAdvanced.AOP;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;

/**
 * @author: cuiH
 * Date: 13-12-10
 */
public class ProxyTest {
    public static void main(String[] args) {
        Class clazzProxy1 = Proxy.getProxyClass(Collection.class.getClassLoader());
        System.out.println(clazzProxy1.getName());      //result: $Proxy0

        /**
         * 代理中带有一个默认的无参的构造方法
         */
        System.out.println();
        System.out.println("constructor 内容分析");
        Constructor[] constructors = clazzProxy1.getConstructors();
        for (Constructor constructor : constructors) {
            StringBuilder stringBuilder = new StringBuilder(constructor.getName());//多线程情况下多使用StringBuffer
            stringBuilder.append('(');
            Class[] clazzParams = constructor.getExceptionTypes();
            for (Class clazzParam : clazzParams) {
                stringBuilder.append(clazzParam.getName()).append(",");
            }
            if (clazzParams.length != 0) {
                stringBuilder.deleteCharAt(stringBuilder.length() - 1);        //去掉最后一个逗号
            }
            stringBuilder.append(')');
            System.out.println(stringBuilder);
        }
        /**
         * result：
         * constructor 内容分析
         * $Proxy0()
         */

        System.out.println();
        System.out.println("method 内容分析");
        Method[] methods = clazzProxy1.getMethods();
        for (Method method : methods) {
            StringBuilder stringBuilder = new StringBuilder(method.getName());//多线程情况下多使用StringBuffer
            stringBuilder.append('(');
            Class[] clazzParams = method.getExceptionTypes();
            for (Class clazzParam : clazzParams) {
                stringBuilder.append(clazzParam.getName()).append(",");
            }
            if (clazzParams.length != 0) {
                stringBuilder.deleteCharAt(stringBuilder.length() - 1);        //去掉最后一个逗号
            }
            stringBuilder.append(')');
            System.out.println(stringBuilder);
        }
        /**
         * result：
         * method 内容分析
         equals()
         toString()
         hashCode()
         isProxyClass()
         getProxyClass(java.lang.IllegalArgumentException)
         getInvocationHandler(java.lang.IllegalArgumentException)
         newProxyInstance(java.lang.IllegalArgumentException)
         wait(java.lang.InterruptedException)
         wait(java.lang.InterruptedException)
         wait(java.lang.InterruptedException)
         getClass()
         notify()
         notifyAll()
         */

        /**
         * 结果分析：JVM动态生成的类，自动包括一个无参的构造方法
         * 和多个类方法
         */
    }
}
